package cn.yanglq.Rss.web.service.impl;

import cn.yanglq.Rss.web.dao.MailSetMapper;
import cn.yanglq.Rss.web.model.MailSet;
import cn.yanglq.Rss.web.model.MailSetExample;
import cn.yanglq.Rss.web.service.MailSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by yanglq on 2018/1/16.
 */
@Service("MailSetService")
public class MailSetServiceImpl implements MailSetService {
    @Autowired
    private MailSetMapper sendInfoMapper;

    public List<MailSet> selectMailInfos(String loginid) {
        MailSetExample example = new MailSetExample();
        MailSetExample.Criteria criteria = example.createCriteria();
//        if (null != startdate) {
//            criteria.andCreatetimeGreaterThanOrEqualTo(startdate);
//        }
//        if (null != enddate) {
//            Calendar calendar = new GregorianCalendar();
//            calendar.setTime(enddate);
//            calendar.add(Calendar.DATE, 1);
//            calendar.add(Calendar.SECOND, -1);
//            criteria.andCreatetimeLessThanOrEqualTo(calendar.getTime());
//        }
        criteria.andUseridEqualTo(loginid);
        List<MailSet> infs = sendInfoMapper.selectByExample(example);
        return infs!=null ? infs : null;
    }

    public int selectMailCount(String loginid, Date startdate, Date enddate) {
        MailSetExample example = new MailSetExample();
        MailSetExample.Criteria criteria = example.createCriteria();
        if (null != startdate) {
            criteria.andCreatetimeGreaterThanOrEqualTo(startdate);
        }
        if (null != enddate) {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(enddate);
            calendar.add(Calendar.DATE, 1);
            calendar.add(Calendar.SECOND, -1);
            criteria.andCreatetimeLessThanOrEqualTo(calendar.getTime());
        }
        criteria.andUseridEqualTo(loginid);
        return sendInfoMapper.countByExample(example);
    }

    public void addMailInfo(MailSet sendinfo) {
        sendInfoMapper.insert(sendinfo);
    }

    public void deleteMailSet(Integer mailid) {
        sendInfoMapper.deleteByPrimaryKey(mailid);
    }
}
